package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.DTO.DishDto;
import com.reggie.DTO.SetmealDto;
import com.reggie.common.R;
import com.reggie.entity.Setmeal;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @GetMapping("/page")
public R<Page> setmealPage(int page,int pageSize){
//        构造分页器
        Page<Setmeal> page1 = new Page<>(page,pageSize);
//        条件筛选器  用于设置查询顺序
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Setmeal::getCode);
//        调用page
setmealService.page(page1,lambdaQueryWrapper);
return R.success(page1);

}

//上传新增套餐
@PostMapping
public R<String> save(@RequestBody Setmeal setmeal){
setmealService.save(setmeal);
return R.success("添加套擦成功");
}
//修改套餐回显
    @GetMapping("/{id}")
    public R<SetmealDto> setmealEdit(@PathVariable Long id){
        SetmealDto set = setmealService.editMessage(id);
        return R.success(set);

    }
//    保存修改 返回信息
    @PutMapping
    public R<String> setmealSave(@RequestBody SetmealDto setmealDto){
setmealService.editSave(setmealDto);
return R.success("保存成功");
    }

//    根据id来停售该数据
    @PostMapping("/status/0")
    public R<String> status(Long ids){
        Setmeal byId = setmealService.getById(ids);
        byId.setStatus(0);
        setmealService.updateById(byId);
        return R.success("已停售");
    }

//    根据id来启售该数据
@PostMapping("/status/1")
public R<String> status1(Long ids){
    Setmeal byId = setmealService.getById(ids);
    byId.setStatus(1);
    setmealService.updateById(byId);
    return R.success("已停售");
}

//    删除与批量删除
    @DeleteMapping
public R<String> deleSetmeal(@RequestParam List<Long> ids){
//        @RequestParam：将请求参数绑定到你控制器的方法参数上（是springmvc中接收普通参数的注解）
        setmealService.deleSetmeal(ids);
return R.success("删除成功");
}
    //app套餐展示页面
//    套餐涉及到一个表的信息  不需要dto
//    套餐归套餐  套餐菜品归套餐菜品分开弄
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
//        Setmeal byId = setmealService.getById(setmeal.getId());
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getCategoryId() !=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        return R.success(list);

    }


}

